<template>
  <div class="card content-box">
    <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="140px" v-auth="'MODIFY_PASSWORD_MODIFY'">
      <el-form-item class="item" :label="t('accessControl.changePassword.loginAccount')" prop="loginName">
        <el-input v-model="ruleForm.loginName" disabled />
      </el-form-item>
      <el-form-item class="item" :label="t('accessControl.changePassword.oldPassword')" prop="oldPassword">
        <el-input v-model="ruleForm.oldPassword" />
      </el-form-item>
      <el-form-item class="item" :label="t('accessControl.changePassword.newPassword')" prop="newPassword">
        <el-input v-model="ruleForm.newPassword" />
      </el-form-item>
      <el-form-item class="item" :label="t('accessControl.changePassword.confirmNewPassword')" prop="enterNewPassword">
        <el-input v-model="ruleForm.enterNewPassword" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)">{{ t("common.save") }}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts" name="dynamicForm">
import { reactive, ref, onMounted } from "vue";
import { checkPhoneNumber, password } from "@/utils/eleValidate";
import type { FormInstance, FormRules } from "element-plus";
import { ElMessage } from "element-plus";
import { findUserDetail, resetPassword, getCurrentLoginUser } from "@/api/modules/accessController";
import { useI18n } from "vue-i18n";

const { t } = useI18n();

const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive({
  loginName: "",
  oldPassword: "",
  newPassword: "",
  enterNewPassword: ""
});

const rules = reactive<FormRules>({
  oldPassword: [{ required: true, message: t("accessControl.changePassword.pleaseEnterOldPassword"), trigger: "blur" }],
  newPassword: [
    {
      required: true,
      validator: password,
      trigger: "blur"
    }
  ],
  enterNewPassword: [
    {
      required: true,
      message: t("accessControl.changePassword.pleaseEnterNewPasswordAgain"),
      trigger: "blur"
    }
  ]
});

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      if (ruleForm.newPassword != ruleForm.enterNewPassword) {
        ElMessage.error(t("accessControl.changePassword.passwordsDoNotMatch"));
        return;
      }
      resetPassword(ruleForm, "editUserPasswordDTO").then(res => {
        if (res.toString() == "true") {
          ElMessage.success(t("accessControl.changePassword.modifySuccess"));
        } else {
          ElMessage.error(t("accessControl.changePassword.modifyFailed"));
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};
onMounted(() => {
  getCurrentLoginUser().then(res => {
    let newRes = JSON.parse(JSON.stringify(res));
    ruleForm.loginName = newRes.loginName;
  });
  // //加载公司信息
  // findUserDetail({ userId: 30 }, "userDto").then(res => {
  //   let newParams = JSON.parse(JSON.stringify(res));
  //   ruleForm.loginName = newParams.loginName;
  // });
});
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>

<style scoped lang="scss">
@import "./index";
</style>
